package com.example.sicnuaccountingapp.ui.category

import android.app.Dialog
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
import com.example.sicnuaccountingapp.R
import com.example.sicnuaccountingapp.data.entity.Category
import com.example.sicnuaccountingapp.databinding.DialogEditCategoryBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class EditCategoryDialog : DialogFragment() {
    private var _binding: DialogEditCategoryBinding? = null
    private val binding get() = _binding!!

    private var category: Category? = null
    private var onSaveClick: ((String, Int?) -> Unit)? = null
    private var selectedIconResourceId: Int? = null

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        _binding = DialogEditCategoryBinding.inflate(LayoutInflater.from(context))

        setupViews()
        setupInitialValues()

        return AlertDialog.Builder(requireContext())
            .setTitle(if (category == null) R.string.add_category else R.string.edit_category)
            .setView(binding.root)
            .setPositiveButton(R.string.save, null)
            .setNegativeButton(R.string.cancel, null)
            .create()
            .apply {
                setOnShowListener { dialog ->
                    (dialog as AlertDialog).getButton(AlertDialog.BUTTON_POSITIVE)
                        .setOnClickListener {
                            saveCategory()
                        }
                }
            }
    }

    private fun setupViews() {
        binding.selectIconButton.setOnClickListener {
            showIconPicker()
        }
    }

    private fun setupInitialValues() {
        category?.let {
            binding.categoryNameEdit.setText(it.name)
            it.iconResourceId?.let { iconId ->
                selectedIconResourceId = iconId
                binding.categoryIcon.setImageResource(iconId)
            }
        }
    }

    private fun showIconPicker() {
        IconPickerDialog.newInstance { iconItem ->
            selectedIconResourceId = iconItem.resourceId
            binding.categoryIcon.setImageResource(iconItem.resourceId)
        }.show(childFragmentManager, IconPickerDialog.TAG)
    }

    private fun saveCategory() {
        val name = binding.categoryNameEdit.text.toString()
        if (name.isBlank()) {
            Toast.makeText(context, R.string.please_input_category_name, Toast.LENGTH_SHORT).show()
            return
        }
        onSaveClick?.invoke(name, selectedIconResourceId)
        dismiss()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    companion object {
        const val TAG = "EditCategoryDialog"

        fun newInstance(
            category: Category? = null,
            onSaveClick: (String, Int?) -> Unit
        ): EditCategoryDialog {
            return EditCategoryDialog().apply {
                this.category = category
                this.onSaveClick = onSaveClick
            }
        }
    }
} 